﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class LocAddrDA : ILocAddrDA
    {
        private string ADDRESS = "@ADDRESS";
        private string CITY = "@CITY";
        private string CONTACT_NAME = "@CONTACT_NAME";
        private string CREATED_BY = "@CREATED_BY";
        private string EMAIL = "@EMAIL";
        private string FAX = "@FAX";
        private string IS_ACTIVE = "@IS_ACTIVE";
        private string IS_WEEKEND_WORK = "@IS_WEEKEND_WORK";
        private string LOC_ADDR_ID = "@LOC_ADDR_ID";
        private string LOC_SITE_ID = "@LOC_SITE_ID";
        private string PROVINCE = "@PROVINCE";
        private string REMARK = "@REMARK";
        private string SQL_CHECK_LOC_ADDR_ID_UNIQUE = " SELECT COUNT(1) FROM @_@LOC_ADDR WHERE LOC_ADDR_ID = @LOC_ADDR_ID ";
        private string SQL_DELETE_LOC_ADDR = " DELETE FROM @_@LOC_ADDR WHERE LOC_ADDR_ID = @LOC_ADDR_ID ";
        private string SQL_INSERT_LOC_ADDR = " INSERT INTO @_@LOC_ADDR ( WH_ID, LOC_ADDR_ID, LOC_SITE_ID, CONTACT_NAME, TELEPHONE, EMAIL, FAX, PROVINCE, CITY, ZIP, ADDRESS, IS_WEEKEND_WORK, REMARK, IS_ACTIVE, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE) VALUES ( @WH_ID, @LOC_ADDR_ID, @LOC_SITE_ID, @CONTACT_NAME, @TELEPHONE, @EMAIL, @FAX, @PROVINCE, @CITY, @ZIP, @ADDRESS, @IS_WEEKEND_WORK, @REMARK, @IS_ACTIVE, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE() )  ";
        private string SQL_SELECT_ALL_LOC_ADDR = " SELECT  ROWID, WH_ID, LOC_ADDR_ID, LOC_SITE_ID, CONTACT_NAME, TELEPHONE, EMAIL, FAX, PROVINCE, CITY, ZIP, ADDRESS, IS_WEEKEND_WORK, REMARK, IS_ACTIVE, TS, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE  FROM  @_@LOC_ADDR ";
        private string SQL_UPDATE_LOC_ADDR = " UPDATE @_@LOC_ADDR SET WH_ID = @WH_ID, LOC_SITE_ID = @LOC_SITE_ID, CONTACT_NAME = @CONTACT_NAME, TELEPHONE = @TELEPHONE, EMAIL = @EMAIL, FAX = @FAX, PROVINCE = @PROVINCE, CITY = @CITY, ZIP = @ZIP, ADDRESS = @ADDRESS, IS_WEEKEND_WORK = @IS_WEEKEND_WORK, REMARK = @REMARK, IS_ACTIVE = @IS_ACTIVE, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE() WHERE LOC_ADDR_ID = @LOC_ADDR_ID  ";
        private string TELEPHONE = "@TELEPHONE";
        private string TS = "@TS";
        private string UPDATED_BY = "@UPDATED_BY";
        private string WH_ID = "@WH_ID";
        private string ZIP = "@ZIP";

        public bool CheckLocAddrIDUnique(string locAddrID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LOC_ADDR_ID", locAddrID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_LOC_ADDR_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckLocAddrIDUnique(DataBase dataBase, DbTransaction tran, string locAddrID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LOC_ADDR_ID", locAddrID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_LOC_ADDR_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public int DeleteLocAddr(List<string> locAddrID, List<string> whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < locAddrID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LOC_ADDR_ID", locAddrID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_LOC_ADDR.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                conn.Close();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int DeleteLocAddr(DataBase dataBase, DbTransaction tran, List<string> locAddrID, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < locAddrID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LOC_ADDR_ID", locAddrID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_LOC_ADDR.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllLocAddr(string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_LOC_ADDR.Replace("@_@", whLoginID);
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetAllLocAddr(DataBase dataBase, DbTransaction tran, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_LOC_ADDR.Replace("@_@", whLoginID);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public LocAddrInfo GetLocAddrByID(string locaddrID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_LOC_ADDR.Replace("@_@", whLoginID) + " WHERE LOC_ADDR_ID = @LOC_ADDR_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LOC_ADDR_ID", locaddrID) };
            LocAddrInfo locAddrInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    locAddrInfo = new LocAddrInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["LOC_ADDR_ID"].ToString(), reader["LOC_SITE_ID"].ToString(), reader["CONTACT_NAME"].ToString(), reader["TELEPHONE"].ToString(), reader["EMAIL"].ToString(), reader["FAX"].ToString(), reader["PROVINCE"].ToString(), reader["CITY"].ToString(), reader["ZIP"].ToString(), reader["ADDRESS"].ToString(), reader["IS_WEEKEND_WORK"].ToString(), reader["REMARK"].ToString(), reader["IS_ACTIVE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
                }
            }
            return locAddrInfo;
        }

        public LocAddrInfo GetLocAddrByID(DataBase dataBase, DbTransaction tran, string locaddrID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_LOC_ADDR.Replace("@_@", whLoginID) + " WHERE LOC_ADDR_ID = @LOC_ADDR_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LOC_ADDR_ID", locaddrID) };
            LocAddrInfo locAddrInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                locAddrInfo = new LocAddrInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["LOC_ADDR_ID"].ToString(), reader["LOC_SITE_ID"].ToString(), reader["CONTACT_NAME"].ToString(), reader["TELEPHONE"].ToString(), reader["EMAIL"].ToString(), reader["FAX"].ToString(), reader["PROVINCE"].ToString(), reader["CITY"].ToString(), reader["ZIP"].ToString(), reader["ADDRESS"].ToString(), reader["IS_WEEKEND_WORK"].ToString(), reader["REMARK"].ToString(), reader["IS_ACTIVE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return locAddrInfo;
        }

        public DataSet GetLocAddrByQueryList(List<string> sqlWhere, LocAddrQueryEntity locaddrQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_LOC_ADDR.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!locaddrQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, locaddrQuery.CurrentPage, locaddrQuery.PageSize, locaddrQuery.SortField, locaddrQuery.SortDirection);
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetLocAddrByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, LocAddrQueryEntity locaddrQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_LOC_ADDR.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            temp = PagingHelper.GetPagingSQL(temp, locaddrQuery.CurrentPage, locaddrQuery.PageSize, locaddrQuery.SortField, locaddrQuery.SortDirection);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public int InsertLocAddr(LocAddrInfo locAddrInfo, List<string> whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    locAddrInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_LocAddr_Parameters(locAddrInfo);
                    if (paras != null)
                    {
                        result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_LOC_ADDR.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                conn.Close();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int InsertLocAddr(DataBase dataBase, DbTransaction tran, LocAddrInfo locAddrInfo, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    locAddrInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_LocAddr_Parameters(locAddrInfo);
                    if (paras != null)
                    {
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_LOC_ADDR.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_LocAddr_Parameters(LocAddrInfo locAddrInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { new SqlParameter(this.WH_ID, SqlDbType.VarChar, 30), new SqlParameter(this.LOC_ADDR_ID, SqlDbType.VarChar, 20), new SqlParameter(this.LOC_SITE_ID, SqlDbType.VarChar, 20), new SqlParameter(this.CONTACT_NAME, SqlDbType.VarChar, 20), new SqlParameter(this.TELEPHONE, SqlDbType.VarChar, 20), new SqlParameter(this.EMAIL, SqlDbType.VarChar, 50), new SqlParameter(this.FAX, SqlDbType.VarChar, 20), new SqlParameter(this.PROVINCE, SqlDbType.VarChar, 20), new SqlParameter(this.CITY, SqlDbType.VarChar, 20), new SqlParameter(this.ZIP, SqlDbType.VarChar, 20), new SqlParameter(this.ADDRESS, SqlDbType.VarChar, 500), new SqlParameter(this.IS_WEEKEND_WORK, SqlDbType.VarChar, 1), new SqlParameter(this.REMARK, SqlDbType.VarChar, 100), new SqlParameter(this.IS_ACTIVE, SqlDbType.VarChar, 1), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 20), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 20) };
            if (!string.IsNullOrEmpty(locAddrInfo.WhID))
            {
                paramArray[0].Value = locAddrInfo.WhID;
            }
            else
            {
                paramArray[0].Value = DBNull.Value;
            }
            paramArray[1].Value = locAddrInfo.LocAddrID;
            paramArray[2].Value = locAddrInfo.LocSiteID;
            if (!string.IsNullOrEmpty(locAddrInfo.ContactName))
            {
                paramArray[3].Value = locAddrInfo.ContactName;
            }
            else
            {
                paramArray[3].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(locAddrInfo.Telephone))
            {
                paramArray[4].Value = locAddrInfo.Telephone;
            }
            else
            {
                paramArray[4].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(locAddrInfo.Email))
            {
                paramArray[5].Value = locAddrInfo.Email;
            }
            else
            {
                paramArray[5].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(locAddrInfo.Fax))
            {
                paramArray[6].Value = locAddrInfo.Fax;
            }
            else
            {
                paramArray[6].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(locAddrInfo.Province))
            {
                paramArray[7].Value = locAddrInfo.Province;
            }
            else
            {
                paramArray[7].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(locAddrInfo.City))
            {
                paramArray[8].Value = locAddrInfo.City;
            }
            else
            {
                paramArray[8].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(locAddrInfo.Zip))
            {
                paramArray[9].Value = locAddrInfo.Zip;
            }
            else
            {
                paramArray[9].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(locAddrInfo.Address))
            {
                paramArray[10].Value = locAddrInfo.Address;
            }
            else
            {
                paramArray[10].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(locAddrInfo.IsWeekendWork))
            {
                paramArray[11].Value = locAddrInfo.IsWeekendWork;
            }
            else
            {
                paramArray[11].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(locAddrInfo.Remark))
            {
                paramArray[12].Value = locAddrInfo.Remark;
            }
            else
            {
                paramArray[12].Value = DBNull.Value;
            }
            paramArray[13].Value = locAddrInfo.IsActive;
            paramArray[14].Value = locAddrInfo.CreatedBy;
            paramArray[15].Value = locAddrInfo.UpdatedBy;
            return paramArray;
        }

        public int UpdateLocAddr(LocAddrInfo locAddrInfo, List<string> whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    locAddrInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_LocAddr_Parameters(locAddrInfo);
                    if (paras != null)
                    {
                        result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_LOC_ADDR.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                conn.Close();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int UpdateLocAddr(DataBase dataBase, DbTransaction tran, LocAddrInfo locAddrInfo, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    locAddrInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_LocAddr_Parameters(locAddrInfo);
                    if (paras != null)
                    {
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_LOC_ADDR.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

